﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 基于WPF的电缆采样数据分析系统.Models.Enums;
using 基于WPF的电缆采样数据分析系统.MsgEvents;
using 基于WPF的电缆采样数据分析系统.Service;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.TDRWork.MenuPage
{
    public class CursorSettingPageUCViewModel : BindableBase
    {
        /// <summary>
        /// 对话框
        /// </summary>
        private readonly DialogHostService DialogHostService;

        /// <summary>
        /// 发布订阅
        /// </summary>
        private readonly IEventAggregator Aggregator;

        /// <summary>
        /// 构造器
        /// </summary>
        public CursorSettingPageUCViewModel(DialogHostService _DialogHostService, IEventAggregator _Aggregator)
        {
            // 绑定对话框
            DialogHostService = _DialogHostService;

            // 绑定发布订阅
            Aggregator = _Aggregator;

            // 绑定主光标X1减少命令
            ZX1_ReduceCmm = new DelegateCommand(ZX1_ReduceFun);

            // 绑定主光标X1增加命令
            ZX1_AddCmm = new DelegateCommand(ZX1_AddFun);

            // 绑定主光标X2减少命令
            ZX2_ReduceCmm = new DelegateCommand(ZX2_ReduceFun);

            // 绑定主光标X2增加命令
            ZX2_AddCmm = new DelegateCommand(ZX2_AddFun);

            // 绑定副光标X1减少命令
            FX1_ReduceCmm = new DelegateCommand(FX1_ReduceFun);

            // 绑定副光标X1增加命令
            FX1_AddCmm = new DelegateCommand(FX1_AddFun);

            // 绑定副光标X2减少命令
            FX2_ReduceCmm = new DelegateCommand(FX2_ReduceFun);

            // 绑定副光标X2增加命令
            FX2_AddCmm = new DelegateCommand(FX2_AddFun);

            // 绑定打开计算器
            OpenCalculatorCmm = new DelegateCommand<CursorValue?>(OpenCalculatorFun);

            // 绑定是否启动光标
            IsCursorStartCmm = new DelegateCommand<string>(IsCursorStart);
        }


        #region 光标面板信息

        /// <summary>
        /// 光标状态
        /// </summary>
        private bool _CursorStatus = false;

        /// <summary>
        /// 光标状态
        /// </summary>
        public bool CursorStatus
        {
            get { return _CursorStatus; }
            set { 
                _CursorStatus = value;
                RaisePropertyChanged();
            }
        }

        #endregion


        #region 主光标数据
        /// <summary>
        /// 主光标X1
        /// </summary>
        private float _ZX1Value = 0;

        /// <summary>
        /// 主光标X1
        /// </summary>
        public float ZX1Value
        {
            get { return _ZX1Value; }
            set { 
                _ZX1Value = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 主光标X2
        /// </summary>
        private float _ZX2Value = 0;

        /// <summary>
        /// 主光标X2
        /// </summary>
        public float ZX2Value
        {
            get { return _ZX2Value; }
            set { 
                _ZX2Value = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 主光标是否显示
        /// </summary>
        private bool _ZTBtn = false;

        /// <summary>
        /// 主光标是否显示
        /// </summary>
        public bool ZTBtn
        {
            get { return _ZTBtn; }
            set { 
                _ZTBtn = value;
                RaisePropertyChanged();
            }
        }


        #endregion


        #region 副光标数据
        /// <summary>
        /// 副光标X1
        /// </summary>
        private float _FX1Value = 0;

        /// <summary>
        /// 副光标X1
        /// </summary>
        public float FX1Value
        {
            get { return _FX1Value; }
            set { 
                _FX1Value = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 副光标X2
        /// </summary>
        private float _FX2Value = 0;

        /// <summary>
        /// 副光标X2
        /// </summary>
        public float FX2Value
        {
            get { return _FX2Value; }
            set { 
                _FX2Value = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 副光标是否显示
        /// </summary>
        private bool _FTBtn = false;

        /// <summary>
        /// 副光标是否显示
        /// </summary>
        public bool FTBtn
        {
            get { return _FTBtn; }
            set { 
                _FTBtn = value;
                RaisePropertyChanged();
            }
        }

        #endregion


        #region 主光标操作

        /// <summary>
        /// 主光标X1减少命令
        /// </summary>
        public DelegateCommand ZX1_ReduceCmm { get; set; }

        /// <summary>
        /// 主光标X1减少方法
        /// </summary>
        private void ZX1_ReduceFun()
        {
            ZX1Value -= 1;
        }

        /// <summary>
        /// 主光标X1增加命令
        /// </summary>
        public DelegateCommand ZX1_AddCmm { get; set; }

        /// <summary>
        /// 主光标X1增加方法
        /// </summary>
        private void ZX1_AddFun()
        {
            ZX1Value += 1;
        }


        /// <summary>
        /// 主光标X2减少命令
        /// </summary>
        public DelegateCommand ZX2_ReduceCmm { get; set; }

        /// <summary>
        /// 主光标X2减少方法
        /// </summary>
        private void ZX2_ReduceFun()
        {
            ZX2Value -= 1;
        }

        /// <summary>
        /// 主光标X2增加命令
        /// </summary>
        public DelegateCommand ZX2_AddCmm { get; set; }

        /// <summary>
        /// 主光标X2增加方法
        /// </summary>
        private void ZX2_AddFun()
        {
            ZX2Value += 1;
        }



        #endregion


        #region 副光标操作
        /// <summary>
        /// 副光标X1减少命令
        /// </summary>
        public DelegateCommand FX1_ReduceCmm { get; set; }

        /// <summary>
        /// 副光标X1减少方法
        /// </summary>
        private void FX1_ReduceFun()
        {
            FX1Value -= 1;
        }

        /// <summary>
        /// 副光标X1增加命令
        /// </summary>
        public DelegateCommand FX1_AddCmm { get; set; }

        /// <summary>
        /// 副光标X1增加方法
        /// </summary>
        private void FX1_AddFun()
        {
            FX1Value += 1;
        }

        /// <summary>
        /// 副光标X2减少命令
        /// </summary>
        public DelegateCommand FX2_ReduceCmm { get; set; }

        /// <summary>
        /// 副光标X2减少方法
        /// </summary>
        private void FX2_ReduceFun()
        {
            FX2Value -= 1;
        }

        /// <summary>
        /// 副光标X2增加命令
        /// </summary>
        public DelegateCommand FX2_AddCmm { get; set; }

        /// <summary>
        /// 副光标X2增加方法
        /// </summary>
        private void FX2_AddFun()
        {
            FX2Value += 1;
        }

        #endregion


        #region 计算器
        /// <summary>
        /// 打开计算器
        /// </summary>
        public DelegateCommand<CursorValue?> OpenCalculatorCmm { get; set; }

        /// <summary>
        /// 打开计算器
        /// </summary>
        private void OpenCalculatorFun(CursorValue? cursor)
        {
            switch (cursor)
            {
                case CursorValue.ZX1:
                    OpenCalculator(ZX1Value, cursor);
                    break;
                case CursorValue.ZX2:
                    OpenCalculator(ZX2Value, cursor);
                    break;
                case CursorValue.FX1:
                    OpenCalculator(FX1Value, cursor);
                    break;
                case CursorValue.FX2:
                    OpenCalculator(FX2Value, cursor);
                    break;
            }
        }

        /// <summary>
        /// 打开计算器内容
        /// </summary>
        private async void OpenCalculator(float value, CursorValue? cursor)
        {
            DialogParameters paras = new DialogParameters();
            paras.Add("ComputeValue", value);
            paras.Add("UnitValue", "ns");
            var result = await DialogHostService.ShowDialog("CalculatorUC", paras);

            if (result.Result == ButtonResult.OK)
            {
                if (result.Parameters.ContainsKey("CurrentCalculatorValue"))
                {
                    // 根据类型更新视图
                    ByCursorTypeChanged(result.Parameters.GetValue<string>("CurrentCalculatorValue"), cursor);
                }
            }
        }

        /// <summary>
        /// 根据类型更新视图
        /// </summary>
        /// <param name="value"></param>
        /// <param name="cursor"></param>
        private void ByCursorTypeChanged(string value, CursorValue? cursor)
        {
            bool result = float.TryParse(value, out float number);

            if (result)
            {
                switch (cursor)
                {
                    case CursorValue.ZX1:
                        ZX1Value = number;
                        break;
                    case CursorValue.ZX2:
                        ZX2Value = number;
                        break;
                    case CursorValue.FX1:
                        FX1Value = number;
                        break;
                    case CursorValue.FX2:
                        FX2Value = number;
                        break;
                }
            }
        }
        #endregion


        #region 是否启动光标
        /// <summary>
        /// 是否启动光标
        /// </summary>
        public DelegateCommand<string> IsCursorStartCmm { get; set; }

        /// <summary>
        /// 是否启动光标
        /// </summary>
        /// <param name="isStart"></param>
        private void IsCursorStart(string isStart)
        {
            if (string.Equals(isStart, "True"))
            {
                CursorStatus = true;
            } else
            {
                CursorStatus = false;

            }

        }

        #endregion
    }
}
